#include "StrList.h"
StringList::StringList()
{
  Data = NULL;
  Next = NULL;
  addr = -1;
}
StringList::~StringList()
{
  if (Data) delete[] Data;
  Data=NULL;
  if (Next) delete Next;
  Next=NULL;
}
StringList *StringList::GetList(int Pos)
{
   StringList *ptr = this;
   while(Pos--)
     ptr = ptr->Next;
   return ptr;
}
int StringList::GetPos(StringList *s)
{
   StringList *ptr = this;
   int pos=0;
   do
   {
     if (ptr==s) return pos;
     ptr = ptr->Next;
     pos++;
   }while(ptr);
   return -1;
}
void StringList::Replace(const char *t)
{
  if (Data) delete[] Data;
  Data = NULL;
  Add(t,addr);
}
void StringList::Add(const char *t,int _addr)
{
  if (Data)
  {
    if (!Next) Next = new StringList;
    Next->Add(t,_addr);
  }
  else
  {
    Data = new char[strlen(t)+1];
    assert(Data);
    strcpy(Data,t);
    addr=_addr;
  }
}
/*-------------------------------------------------------------
Insert - inserts the given string in the current position and
 pushes everything else down by one unit.
*** Data in 'this' stringlist is replaced by the new one.
-------------------------------------------------------------*/
void StringList::Insert(const char *t)
{
  if (Data)
  {
    StringList *tmp = Next;
    Next = new StringList;
    Next->Add(Data,addr);
    Next->Next = tmp;
    delete[] Data;
    Data=NULL;
  }
  Add(t);
}
void StringList::Delete(StringList *s)
{
  assert(s!=this);
  StringList *ptr = this;
  while(ptr->Next!=s) ptr = ptr->Next;
  ptr->Next = s->Next;
  s->Next = NULL;
  delete s;
}